package com.alex.service.r;

import cn.hutool.core.io.FileUtil;
import com.alex.database.entity.Pmr;
import com.alex.database.mapper.PmrMapper;
import com.alex.utils.CacheUtil;
import com.alex.utils.MyUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2024/3/14 20:34
 */
@Service
@Slf4j
public class AsyncService {

    @Resource
    PmrMapper pmrMapper;


    @Resource
    CacheUtil cacheUtil;

    // 可以点击的图形
    @Async
    public void dealWosProgressByClick(String FileStr, String rootPath, Integer userUid, String term, String term_md5, List<String> resultList) {
        log.info("WosByClick异步进来了");
        String redisKey = userUid + term_md5;
        String redisKeyRes = userUid + term_md5 + "res";
        long l = System.currentTimeMillis();
        String UserPath = rootPath + userUid + "/" + l + "/";
        Pmr ins = new Pmr();
        ins.setUid(userUid);
        ins.setTerm(term);
        ins.setTermMd5(term_md5);
        ins.setStatus(1);
        ins.setPath(UserPath);
        ins.setWosPath(UserPath);
        ins.setType(2);
        pmrMapper.insert(ins);
        //异步不能使用基于session 存储的redis
        cacheUtil.saveObjectByRedis(redisKey, 1);
        //拿取R 文件模板替换路径 写入文件
        String rTemp = FileUtil.readUtf8String("/home/pubmedr/R/WosMainClick.R");
        String rCode = rTemp.replace("gcr", userUid + "").replace("20231116", l + "");
        FileUtil.writeUtf8String(rCode, UserPath + "Main.R");
        FileUtil.copy("/home/pubmedr/R/utils.R", UserPath + "utils.R", true);
        FileUtil.writeUtf8String(FileStr, UserPath + "savedrecsre1.txt");
        // 调用R 执行代码
        String command = "docker exec rbase Rscript " + UserPath + "Main.R";
        log.info("执行脚本路径+" + command);
        try {
            Process process = Runtime.getRuntime().exec(command);
            int exitCode = process.waitFor();
            log.info("Exit code: " + exitCode);
            if (exitCode == 1) {
                List<Pmr> pmrsGOTO = pmrMapper.selectList(new QueryWrapper<Pmr>().eq("uid", userUid).eq("term_md5", term_md5));
                for (Pmr pmr : pmrsGOTO) {
                    pmr.setStatus(3);
                    pmrMapper.updateById(pmr);
                }
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        List<File> hwImageall = MyUtils.getExcelAll(UserPath);
        for (File file : hwImageall) {
            resultList.add(file.getPath());
        }
        List<Pmr> pmrsGOTO = pmrMapper.selectList(new QueryWrapper<Pmr>().eq("uid", userUid).eq("term_md5", term_md5).ne("status", 4));
        if (pmrsGOTO.size() > 0) {
            Pmr pmr = pmrsGOTO.get(0);
            log.info("画图click点击+" + pmr.getId() + "完毕getStatus+" + pmr.getStatus());
            if (hwImageall.size() > 0) {
                pmr.setStatus(2);
                pmr.setRes(JSONObject.toJSONString(resultList));
                cacheUtil.saveObjectByRedis(redisKey, 2);
                log.info("处理成功保存装redisKey+" + redisKey);
                cacheUtil.saveObjectByRedis(redisKey + "path", pmr.getPath());
                cacheUtil.saveObjectByRedis(redisKeyRes, JSONObject.toJSONString(resultList));

            } else {
                pmr.setStatus(4);
                cacheUtil.saveObjectByRedis(redisKey, 4);
            }
            pmrMapper.updateById(pmr);
        } else {
            cacheUtil.saveObjectByRedis(redisKey, 3);
        }
    }
}
